package com.wson.function.compass;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;

import com.wson.behavior.layout.IWindow;
import com.wson.constant.IRuntimeConstant;
import com.wson.function.basic.BasicFunc;
import com.wson.root.util.LoggerUtil;

 class MailNotificationFunc extends BasicFunc {


    private JavaMailSender mailSender;
    
    public void function(final String targets, final String subject, final IWindow window){
    	this.function(targets, subject, window.generateBehaviorCode());
    }
	public void function(final String targets, final String subject, final String content){
		 MimeMessagePreparator preparator = new MimeMessagePreparator()
         {
              @Override
              public void prepare(MimeMessage mimeMessage) throws Exception
              {
                   MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                   message.setTo(targets);
                   
                   message.setSubject(subject);
                   //message.setFrom("");
                   String text = content;
                   message.setText(text, true);
                   LoggerUtil.DEBUG("sent mail - subject : " + subject + " targets :" + targets);

              }
         };

         try
         {
        	 if(mailSender==null)
        		 mailSender=(JavaMailSender)this.getSystemProfile().getPreference(IRuntimeConstant.MAILSENDER);
        	 LoggerUtil.DEBUG("mailSender "+mailSender);
              mailSender.send(preparator);
         } catch (MailException e)
         {
        	 LoggerUtil.DEBUG(e);
         }
	}
	

}
